//
//  TTBaseVC.swift
//  TianTianBiTing
//
//  Created by Gox mac 2 on 2018/12/17.
//  Copyright © 2018 Gox_WallyDiao. All rights reserved.
//

import UIKit

class TTBaseVC: UIViewController {
    fileprivate var keyBoredTagView:UIView!
    
    //  MARK: - ###### 产品寿命 ######
    override func viewDidLoad() {
        super.viewDidLoad()
        
//        self.navigationController?.interactivePopGestureRecognizer?.isEnabled = false
    
        self.navigationController?.navigationBar.shadowImage = UIImage.init()
        let item = UIBarButtonItem(title: "", style: .plain, target: self, action: nil)
        self.navigationItem.backBarButtonItem = item
        self.view.backgroundColor = WhiteBackColor
        
        self.keyBoredTagView = self.view
        
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    // MARK: addkeyBoardObserver
    func addkeyBoardObserver(tagView:UIView) {
        
        self.keyBoredTagView = tagView
        NotificationCenter.default.addObserver(self, selector: #selector(keyBoardWillShow(notification:)), name:UIResponder.keyboardWillShowNotification, object: nil)
        
        NotificationCenter.default.addObserver(self, selector: #selector(keyBoardWillHide(notification:)), name:UIResponder.keyboardWillHideNotification, object: nil)
    }
    
    func removeKeyboardObserver()  {
        NotificationCenter.default.removeObserver(self)
    }
    
    
    
}

//  MARK: - ###### 私有方法 ######
extension TTBaseVC {
    
    @objc func keyBoardWillShow(notification: Notification){
        
        
        let kbInfo = notification.userInfo
        
        let kbRect = (kbInfo?[UIResponder.keyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
        print(kbRect)
        
        let changeY = kbRect.origin.y - (VIEW_Y(view: self.keyBoredTagView) + VIEW_HEIGHT(view: self.keyBoredTagView))
        
        let duration = kbInfo?[UIResponder.keyboardAnimationDurationUserInfoKey] as!Double
        
        self.keyBoardOffset(y: changeY, duration: duration)
    }
    
    
    @objc fileprivate func keyBoardWillHide(notification: Notification){
        
        let kbInfo = notification.userInfo
        
        let kbRect = (kbInfo?[UIResponder.keyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
        
        let changeY = kbRect.origin.y - SCREEN_HEIGHT + OFFSET_TOP
        
        let duration = kbInfo?[UIResponder.keyboardAnimationDurationUserInfoKey] as!Double
        
        self.keyBoardOffset(y: changeY, duration: duration)
        
    }
    
    fileprivate func keyBoardOffset(y:CGFloat, duration:Double)  {
        UIView.animate(withDuration: duration) {
            
            self.view.frame = CGRect.init(x: VIEW_X(view: self.view), y: y, width: VIEW_WIDTH(view: self.view), height: VIEW_HEIGHT(view: self.view))
        }
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        self.view.endEditing(true)
    }
    
}
